<style lang="stylus" scoped>
  .h-td-maxwidth
    max-width 150px
  // 去除table header checkbox 所在td的top border
  .q-table--cell-separator td, .q-table--cell-separator th
    border-width 1px
    border-top-width 0
  .expand-row-label
    display inline-block
    width 80px
    text-align left
</style>

<template>
  <div>
    <q-table
      class="bg-white"
      dense
      :data="data"
      :columns="columns"
      row-key="name"
      separator="cell"
      selection="multiple"
      :selected.sync="selected"
      :visible-columns="visibleColumns"
      table-class="h-custom"
    >
      <template v-slot:top-left="props">
        <q-btn size="sm" color="light-blue" :label="$t('新增')"/>
      </template>
      <template v-slot:top-right="props">
        <q-select
          v-model="visibleColumns"
          multiple
          borderless
          options-dense
          :display-value="$t('显示列')"
          emit-value
          map-options
          :options="columns"
          option-value="name"
          class="q-mx-lg"
        >
          <template v-slot:option="scope">
            <q-item
              v-bind="scope.itemProps"
              v-on="scope.itemEvents"
              v-if="!scope.opt.required"
            >
              <q-item-section avatar>
                <q-toggle color="blue" v-model="visibleColumns" :val="scope.opt.name" />
              </q-item-section>
              <q-item-section style="min-width:60px">
                {{ scope.opt.label }}
              </q-item-section>
            </q-item>
          </template>
        </q-select>
        <q-toggle
          v-model="searchToggle"
          left-label
          label="搜索"
          color="blue"
          checked-icon="check"
          unchecked-icon="clear"
          keep-color
        />
        <q-btn
          flat round dense
          :icon="props.inFullscreen ? 'fullscreen_exit' : 'fullscreen'"
          @click="props.toggleFullscreen"
          class="q-ml-md"
        />
      </template>
      <template v-slot:top-row="props" v-if="searchToggle">
        <q-tr :props="props">
          <q-td>
          </q-td>
          <q-td v-for="col in props.cols" :key="col.name" class="">
            <q-btn color="primary" dense flat label="搜索" v-if="col.name==='action'"/>
            <q-input dense outlined v-model="searchForm.name" v-else/>
          </q-td>
        </q-tr>
      </template>
      <template v-slot:header="props">
        <q-tr :props="props">
          <q-th>
            <q-checkbox indeterminate-value="some" dense v-model="props.selected" />
          </q-th>
          <q-th v-for="col in props.cols" :key="col.name" :props="props">
            # {{ col.label }} #
          </q-th>
        </q-tr>
      </template>
      <template v-slot:body="props">
        <q-tr :props="props">
          <q-td>
            <q-checkbox dense v-model="props.selected" />
          </q-td>
          <q-td key="action" :props="props">
            <q-btn-dropdown
              color="blue"
              size="sm"
              dense
              menu-anchor="bottom left"
              menu-self="top left"
              content-class="text-grey"
              icon="settings">
              <q-list dense>
                <q-item clickable v-close-popup>
                  <q-item-section>
                    <q-item-label>修改</q-item-label>
                  </q-item-section>
                </q-item>
                <q-item clickable v-close-popup>
                  <q-item-section>
                    <q-item-label>删除</q-item-label>
                  </q-item-section>
                </q-item>
                <q-item clickable v-close-popup @click="props.expand = !props.expand">
                  <q-item-section>
                    <q-item-label>{{ props.expand? '收起': '更多字段' }}</q-item-label>
                  </q-item-section>
                </q-item>
              </q-list>
            </q-btn-dropdown>
          </q-td>
          <q-td key="name" :props="props">
            {{ props.row.name }}
            <q-btn dense round flat :icon="props.expand ? 'arrow_drop_up' : 'arrow_drop_down'" @click="props.expand = !props.expand" />
          </q-td>
          <q-td key="calories" :props="props">
            {{ props.row.calories }}
            <q-popup-edit v-model="props.row.calories" title="Update calories" buttons>
              <q-input type="number" v-model="props.row.calories" dense autofocus />
            </q-popup-edit>
          </q-td>
          <q-td key="fat" :props="props">
            <div class="text-pre-wrap">{{ props.row.fat }}</div>
            <q-popup-edit v-model="props.row.fat">
              <q-input type="textarea" v-model="props.row.fat" dense autofocus />
            </q-popup-edit>
          </q-td>
          <q-td key="carbs" :props="props">
            {{ props.row.carbs }}
            <q-popup-edit v-model="props.row.carbs" title="some text" buttons persistent>
              <q-input type="number" v-model="props.row.carbs" dense autofocus hint="Use buttons to close" />
            </q-popup-edit>
          </q-td>
          <q-td key="protein" :props="props">
            <q-chip dense square label="成功类" color="positive" text-color="white" v-if="props.row.protein==='成功类'"/>
            <q-chip dense square label="失败类" color="negative" text-color="white" v-if="props.row.protein==='失败类'"/>
            <q-chip dense square label="预警类" color="warning" text-color="white" v-if="props.row.protein==='预警类'"/>
            <q-chip dense square label="未开始" color="grey" text-color="white" v-if="props.row.protein==='未开始'"/>
            <q-chip dense square label="待审核" color="grey" text-color="white" v-if="props.row.protein==='待审核'"/>
            <q-chip dense square label="信息类" color="info" text-color="white" v-if="props.row.protein==='信息类'"/>
          </q-td>
          <q-td key="sodium" :props="props" class="h-td-maxwidth ellipsis">
            {{ props.row.sodium }}
            <q-popup-edit v-model="props.row.sodium">
              <q-input type="textarea" v-model="props.row.sodium" readonly dense autofocus counter />
            </q-popup-edit>
          </q-td>
          <q-td key="calcium" :props="props">{{ props.row.calcium }}</q-td>
          <q-td key="iron" :props="props">{{ props.row.iron }}</q-td>
        </q-tr>
        <q-tr v-show="props.expand" :props="props">
          <q-td colspan="100%">
            <div class="row justify-between">
              <div class="col-6 q-my-sm">
                <strong class="expand-row-label">创建人：</strong>
                <span>{{ props.row.createUser }}</span>
              </div>
              <div class="col-6 q-my-sm">
                <strong class="expand-row-label">创建时间：</strong>
                <span>{{ props.row.createTime }}</span>
              </div>
              <div class="col-12 q-my-sm">
                <strong class="expand-row-label">说 明：</strong>
                <span>{{ props.row.remark }}</span>
              </div>
            </div>
          </q-td>
        </q-tr>
      </template>
    </q-table>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchForm: {
        name: ''
      },
      searchToggle: false,
      selected: [],
      visibleColumns: [
        'action',
        'name',
        'calories',
        'fat',
        'carbs',
        'protein',
        'sodium',
        'calcium',
        'iron'
      ],
      columns: [
        { name: 'action', align: 'center', label: '操作' },
        {
          name: 'name',
          required: true, // 当加入此属性时  visibleColumns 对当前字段无效
          label: '文字左对齐 - 示例',
          align: 'left',
          field: 'name'
        },
        { name: 'calories', align: 'center', label: 'pop编辑方式 - 示例', field: 'calories' },
        { name: 'fat', label: '可排序 - 示例', field: 'fat', sortable: true },
        { name: 'carbs', label: '数字右对齐 - 示例', field: 'carbs' },
        { name: 'protein', align: 'center', label: '状态chip - 示例', field: 'protein' },
        { name: 'sodium', label: '固定宽度-超出省略', field: 'sodium' },
        { name: 'calcium', label: 'Calcium (%)', field: 'calcium', sortable: true, sort: (a, b) => parseInt(a, 10) - parseInt(b, 10) }
      ],
      data: [
        {
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: '成功类',
          sodium: '从前有座山，山上有个庙，庙里有一个老和尚和一个小和尚，老和尚在给小和尚讲故事，讲什么故事呢？',
          calcium: '3%',
          iron: '8%',
          createTime: '2019-03-19',
          createUser: 'Yangdong.Hu',
          remark: 'Lorem, ipsum dolor sit amet consectetur adipisicing elit. Iusto temporibus, accusantium voluptatibus modi facilis ex beatae atque earum blanditiis voluptatem ea natus molestiae laborum velit.'
        },
        {
          name: 'Gingerbread',
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: '待审核',
          sodium: 327,
          calcium: '7%',
          iron: '16%'
        },
        {
          name: 'Donut',
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: '信息类',
          sodium: 326,
          calcium: '2%',
          iron: '22%'
        },
        {
          name: 'Lollipop',
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: '失败类',
          sodium: 38,
          calcium: '0%',
          iron: '2%'
        },
        {
          name: 'Honeycomb',
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: '预警类',
          sodium: 562,
          calcium: '0%',
          iron: '45%'
        },
        {
          name: 'Jelly bean',
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: '成功类',
          sodium: 50,
          calcium: '0%',
          iron: '0%'
        },
        {
          name: 'KitKat',
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: '未开始',
          sodium: 54,
          calcium: '12%',
          iron: '6%'
        }
      ]
    }
  }
}
</script>
